package com.whfc.openapi.api.face.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.whfc.emp.enums.FaceGateSyncOp;
import com.whfc.emp.enums.FaceGateType;
import com.whfc.emp.enums.YfFaceGateSyncType;
import com.whfc.emp.param.EmpAttendSyncDataParam;
import com.whfc.emp.param.EmpInfoSyncDataParam;
import com.whfc.emp.param.openapi.OpenApiEmpSyncParam;
import com.whfc.fuum.dto.open.OpenApiLicenseDTO;
import com.whfc.fuum.service.OpenApiService;
import com.whfc.openapi.api.face.service.FaceGateSyncService;
import com.whfc.openapi.api.face.util.FaceGateDataConverter;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description 宇泛智能-离线设备
 * @Author hw
 * @Date 2021/1/28 11:33
 * @Version 1.0
 */
@RestController
@RequestMapping("/open/api/faceGate/yf")
public class FaceGateYFOfflineController {


    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @DubboReference(interfaceClass = OpenApiService.class, version = "1.0.0")
    private OpenApiService openApiService;

    @Autowired
    private FaceGateSyncService faceGateSyncService;

    /**
     * 同步处理
     */
    @PostMapping("/{appKey}")
    public String handleRec(@RequestBody String body, @PathVariable("appKey") String appKey) {
        logger.info("宇泛离线闸机,人员同步,body:{}", body);
        String resultStr = "{\"result\":1,\"success\":true}";
        //验证AppKey
        OpenApiLicenseDTO licenseDTO = openApiService.getOpenApiLicense(appKey);
        if (licenseDTO == null) {
            return resultStr;
        }
        JSONObject json = JSONObject.parseObject(body);
        if (json == null) {
            return resultStr;
        }
        Integer deptId = licenseDTO.getDeptId();
        Integer type = json.getInteger("type");
        String content = json.getString("content");
        if (YfFaceGateSyncType.SYNC_ATTEND.getValue().equals(type)) {
            EmpAttendSyncDataParam dataParam = FaceGateDataConverter.attendDataConverter(content);
            if (dataParam != null) {
                dataParam.setDeptId(deptId);
                dataParam.setPlatform(FaceGateType.YFOFFLINE.getCode());
                faceGateSyncService.syncAttend(dataParam);
            }
        } else {
            if (YfFaceGateSyncType.SYNC_EMP_ADD.getValue().equals(type) ||
                    YfFaceGateSyncType.SYNC_EMP_UPDATE.getValue().equals(type)) {
                OpenApiEmpSyncParam empParam = JSONObject.parseObject(content, OpenApiEmpSyncParam.class);
                EmpInfoSyncDataParam dataParam = FaceGateDataConverter.empInfoDataConverter(empParam);
                if (dataParam == null) {
                    return resultStr;
                }
                dataParam.setDeptId(deptId);
                dataParam.setPlatform(FaceGateType.YFOFFLINE.getCode());
                dataParam.setSyncType(FaceGateSyncOp.EMP_ADD_OR_UPDATE.getValue());
                faceGateSyncService.syncEmpInfo(dataParam);
            } else if (YfFaceGateSyncType.SYNC_EMP_DEL.getValue().equals(type)) {
                //删除人员
                JSONObject contentObe = JSONObject.parseObject(content);
                JSONArray jsonArray = contentObe.getJSONArray("employeeNos");
                if (jsonArray == null || jsonArray.isEmpty()) {
                    return resultStr;
                }
                for (Object obj : jsonArray) {
                    EmpInfoSyncDataParam dataParam = new EmpInfoSyncDataParam();
                    dataParam.setSyncType(FaceGateSyncOp.EMP_DEL.getValue());
                    dataParam.setEmpCode(String.valueOf(obj));
                    dataParam.setDeptId(deptId);
                    dataParam.setPlatform(FaceGateType.YFOFFLINE.getCode());
                    faceGateSyncService.syncEmpInfo(dataParam);
                }
            }
        }
        return resultStr;
    }

}
